package com.ybliang.plat.common.rpc.protocol.json;

import com.ybliang.plat.common.rpc.core.RpcResponse;
import com.ybliang.plat.common.rpc.protocol.Serializer;

import java.util.LinkedHashMap;
import java.util.List;

/**
 * @author liangyb1
 */
public class JSONSerializer implements Serializer {

    @Override
    public byte[] serialize(Object object) throws Exception {
        return JSON.toJSONBytes(object);
    }

    @Override
    public <T> T deserialize(Class<T> clazz, byte[] bytes) throws Exception {
        return JSON.parseObject(bytes, clazz);
//        T t = JSON.parseObject(bytes, clazz);
//        boolean isRpcResponse = (t instanceof RpcResponse);
//        if(!isRpcResponse) {
//            return t;
//        }
//        RpcResponse rpcResponse = (RpcResponse) t;
//        String resultJson = JSON.toJSONString(rpcResponse.getResult());
//        boolean isJsonArray = resultJson.startsWith("[");
//        if(isJsonArray) {
//            return t;
//        }
//
//        //转化为JsonArray
//        resultJson = "[" + resultJson + "]";
//        rpcResponse.setResult(JSON.parseObject(resultJson, List.class));
//        rpcResponse.setAssemblyJsonArray(true);
//        return (T) rpcResponse;
    }
}
